package jmine.tec.persist.diagnostician;

import java.util.concurrent.Callable;

import bancosys.tec.persist.controller.TransactionalController;
import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;

/**
 * @author Marcio Ribeiro (mmr)
 * @created Dec 19, 2011
 */
public abstract class TransactedDiagnostician extends AbstractDiagnostician {

    private final TransactionalController controller;

    /**
     * Constructor.
     * 
     * @param controller transactional controller.
     */
    public TransactedDiagnostician(TransactionalController controller) {
        this.controller = controller;
    }

    /**
     * Execute the diagnostician inside a transaction.
     * 
     * @param bean the diagnosisbean.
     * @return <code>true</code> if the execution executed without errors, <code>false</code> otherwise.
     * @throws Exception in case something unexpected happens.
     */
    @Override
    protected final boolean fillDiagnosisBean(final DiagnosisBean bean) throws Exception {
        return controller.executeTransacted(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                return doTransacted(bean);
            }

        });
    }

    /**
     * Code inside this method are going to be executed inside a transaction.
     * 
     * @param bean diagnosis bean.
     * @return <code>true</code> if the execution executed without errors, <code>false</code> otherwise.
     * @throws Exception in case something unexpected happens.
     */
    protected abstract boolean doTransacted(DiagnosisBean bean) throws Exception;

    /**
     * @return the controller.
     */
    protected TransactionalController getController() {
        return this.controller;
    }
}
